/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VooCargas.java
 *
 * Created on 2/Jun/2011, 2:06:25
 */

package Interface;

import Classes.Carga;
import aerogest.AerogestSistema;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

/**
 *
 * @author goku
 */
public class VooCargas extends javax.swing.JDialog {

    private static String codVoo;
    private static AerogestSistema as;
    private static GregorianCalendar da;
    /** Creates new form VooCargas */
    public VooCargas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        init();
    }

    private void estado1(){
        jLabel_Cargas_adicionadas.setText(""+as.getVoosPorDia(da).get(codVoo).getCargasCarregadas().size());
        jLabel_Carregamento.setText("");
        jLabel_Carregamento_Total.setText(""+as.getVoosPorDia(da).get(codVoo).getTempoCarregamento());
        jLabel_Numero_Cargas.setText(""+as.getVoosPorDia(da).get(codVoo).getCarga().size());

        jButton_Carregar_Select.setEnabled(false);
    }

    private void init(){
        jLabel_Codigo.setText(codVoo);
        jList2.setListData(as.getVoosPorDia(da).get(codVoo).getCarga().toArray());
        GregorianCalendar g1 = as.getVoosPorDia(da).get(codVoo).getHoraPreparacao1();
        g1.add(GregorianCalendar.MINUTE, (int) as.getVoosPorDia(da).get(codVoo).getTempoCarregamento());
        long time = da.getTimeInMillis() - g1.getTimeInMillis();
        long minutos = (time%(60*60*1000))/(60*1000);
        long segundos = ((time%(60*60*1000))%(60*1000))/1000;

        jLabel_Carregamento_Restante.setText(""+minutos+":"+segundos);
        estado1();
    }

    private void actualizar(){
        jLabel_Cargas_adicionadas.setText(""+as.getVoosPorDia(da).get(codVoo).getCargasCarregadas().size());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel_Codigo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel_Numero_Cargas = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel_Cargas_adicionadas = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel_Carregamento_Total = new javax.swing.JLabel();
        jButton_Carregar_Select = new javax.swing.JButton();
        jButton_Carregar_Todas = new javax.swing.JButton();
        jButton_Voltar = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel_Carregamento = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel_Carregamento_Restante = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Codigo do Voo :");

        jLabel_Codigo.setText("jLabel2");

        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jList2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        jLabel8.setText("Carga:");

        jLabel3.setText("Numero de Cargas:");

        jLabel_Numero_Cargas.setText("jLabel2");

        jLabel5.setText("Cargas Adicionadas:");

        jLabel_Cargas_adicionadas.setText("jLabel2");

        jLabel7.setText("Tempo De Carregamento Total");

        jLabel_Carregamento_Total.setText("jLabel2");

        jButton_Carregar_Select.setText("Carregar Seleccionada");
        jButton_Carregar_Select.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Carregar_SelectActionPerformed(evt);
            }
        });

        jButton_Carregar_Todas.setText("Carregar Todas");
        jButton_Carregar_Todas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Carregar_TodasActionPerformed(evt);
            }
        });

        jButton_Voltar.setText("Voltar");
        jButton_Voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_VoltarActionPerformed(evt);
            }
        });

        jLabel9.setText("Tempo De Carregamento");

        jLabel_Carregamento.setText("jLabel2");

        jLabel10.setText("Tempo De Carr. Restante");

        jLabel_Carregamento_Restante.setText("jLabel2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(34, 34, 34)
                        .addComponent(jLabel_Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButton_Carregar_Todas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton_Carregar_Select, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel10))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_Carregamento_Restante, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(jLabel_Numero_Cargas, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(jLabel_Cargas_adicionadas, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(jLabel_Carregamento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(jLabel_Carregamento_Total, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                                    .addComponent(jButton_Voltar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(12, 12, 12))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel_Codigo))
                .addGap(12, 12, 12)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel_Numero_Cargas))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel_Cargas_adicionadas))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel_Carregamento))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel_Carregamento_Total))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel_Carregamento_Restante))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton_Carregar_Select)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton_Carregar_Todas))
                            .addComponent(jButton_Voltar))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList2MouseClicked
        jLabel_Carregamento.setText(""+as.getVoosPorDia(da).get(codVoo).getCarga().
                get(jList2.getSelectedIndex()).getTempoCarregamento());
        Carga s = (Carga)jList2.getSelectedValue();
        ArrayList<String> ss = as.getVoosPorDia(da).get(codVoo).getCargasCarregadas();
        if (!ss.contains(s.getCodigo()))
            jButton_Carregar_Select.setEnabled(true);
        else
            jButton_Carregar_Select.setEnabled(false);
    }//GEN-LAST:event_jList2MouseClicked

    private void jButton_Carregar_SelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_Carregar_SelectActionPerformed
        Carga s = (Carga)jList2.getSelectedValue();
        ArrayList<Carga> ss = as.getVoosPorDia(da).get(codVoo).getCarga();

        as.getVoosPorDia(da).get(codVoo).embarqueCarga(s);

        actualizar();
        jList2.setSelectedValue(null, false);
        jButton_Carregar_Select.setEnabled(false);

         if (as.getVoosPorDia(da).get(codVoo).getCarga().size() ==
                as.getVoosPorDia(da).get(codVoo).getCargasCarregadas().size())
            JOptionPane.showMessageDialog(rootPane, "Cargas já todas carregadas");
    }//GEN-LAST:event_jButton_Carregar_SelectActionPerformed

    private void jButton_Carregar_TodasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_Carregar_TodasActionPerformed
        if (as.getVoosPorDia(da).get(codVoo).getCarga().size() ==
                as.getVoosPorDia(da).get(codVoo).getCargasCarregadas().size())
            JOptionPane.showMessageDialog(rootPane, "Cargas já todas carregadas");
        else
            as.getVoosPorDia(da).get(codVoo).embarqueCargaALL();
        actualizar();
    }//GEN-LAST:event_jButton_Carregar_TodasActionPerformed

    private void jButton_VoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_VoltarActionPerformed
        dispose();
    }//GEN-LAST:event_jButton_VoltarActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String codVoo, AerogestSistema as,GregorianCalendar da) {
        VooCargas.codVoo = codVoo;
        VooCargas.as = as;
        VooCargas.da = da;
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VooCargas dialog = new VooCargas(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Carregar_Select;
    private javax.swing.JButton jButton_Carregar_Todas;
    private javax.swing.JButton jButton_Voltar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabel_Cargas_adicionadas;
    private javax.swing.JLabel jLabel_Carregamento;
    private javax.swing.JLabel jLabel_Carregamento_Restante;
    private javax.swing.JLabel jLabel_Carregamento_Total;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Numero_Cargas;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}
